from diff import *
import matplotlib.pyplot as plt

#画出ETF基金与对应股票的价差
fig1 = plt.figure()
plt.plot(dates,diff)
plt.title('价差')

#根据可套利点的价差阈值求出可套利区间范围
startpoint = None
endpoint = None
interval_tuples = []
for i in range(len(diff)):
    if diff[i] >0.19:
        if startpoint is None:
            startpoint = i

    elif diff[i] <= 0.19 and startpoint is not None:
        endpoint = i
        if startpoint>endpoint:
            startpoint,endpoint = endpoint,startpoint
        interval_tuples.append((startpoint,endpoint))
        #左闭右开区间
        startpoint = None

print(interval_tuples)#这是提取了区间哦！！可套利位置的区间

# 根据可套利区间求出对应点位的斜率
extracted_data = []
for interval in interval_tuples:
    start = interval[0]
    end = interval[1]
    sub_array = diff1[start:end]
    extracted_data.extend(sub_array)
print(extracted_data)
absextdata = []
for i in extracted_data:
    absextdata.append(abs(i))


#求出非套利点区间以及对应点位的斜率
leftdata = []
for i in range(len(diff1)):
    included = False
    for interval in interval_tuples:
        start = interval[0]
        end = interval[1]
        if start<= i < end:
            included = True
            break
    if not included:
        leftdata.append(diff1[i])

absleftdata = []
for i in leftdata:
    absleftdata.append(abs(i))

#画出可套利点的斜率折线图以及不可套利点的斜率折线图，比较找出区分可套利点的斜率
fig2 = plt.figure()
plt.plot(range(len(absextdata)),absextdata)

fig3 = plt.figure()
plt.plot(range(len(absleftdata)),absleftdata)
plt.show()


